﻿ using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class Lesson11 : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        Toggle tog = this.GetComponent<Toggle>();
        if (tog)
        {
            tog.isOn = false;
            tog.onValueChanged.AddListener((isOn)=>
            {
                Debug.LogFormat("Listener 状态改变:{0}", isOn);
            });
            //添加监听
            tog.onValueChanged.AddListener(ChangeValue2);
            //删除监听
            tog.onValueChanged.RemoveListener(ChangeValue2);
            //删除所有监听
            tog.onValueChanged.RemoveAllListeners();

        }

        ToggleGroup toggleGroup = this.GetComponent<ToggleGroup>();
        if (toggleGroup)
        {
            toggleGroup.allowSwitchOff = false;
            //遍历迭代器,得到选中的单选框
            foreach (Toggle item in toggleGroup.ActiveToggles())
            {
                Debug.LogFormat("tog_name:{0} ison:{1}", item.name, item.isOn);
            }
        }
    }

    public void ChangeValue(bool isOn)
    {
        Debug.LogFormat("ChangeValue 状态改变:{0}", isOn);
    }

    private void ChangeValue2(bool isOn)
    {
        Debug.LogFormat("ChangeValue2 状态改变:{0}", isOn);
    }
}
